<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 实现页面的基本布局 -->
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <TreeTools :tree-node="company" :isRoot="true" @addDepts="addDepartments"/>
        <!-- 下面就是树形控件内容了 -->
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <!-- 传入内容 插槽内容 会循环多次 有多少节点 就循环多少次 -->
          <!-- 作用域插槽 slot-scope="obj" 接收传递给插槽的数据   data 每个节点的数据对象-->
          <TreeTools slot-scope="{ data }" :treeNode="data" @delDepts="getDepartments" @addDepts="addDepartments"/>
        </el-tree>
      </el-card>
    </div>
    <AddDept :showDialog="dialogShow" :tree-node="node" @addDepts="getDepartments" @closeDialog="dialogShow=false"/>
  </div>
</template>

<script>
import TreeTools from './components/tree-tools'
import AddDept from './components/add-dept'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'

export default {
  name: 'Departments',
  components: { TreeTools, AddDept },
  data() {
    return {
      company: {}, // 公司名称
      defaultProps: {
        label: 'name'
      },
      departs: [], // 部门信息
      dialogShow: false,
      node: null //当前点击的部门
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const result = await getDepartments()
      console.log(result)
      this.company = { name: result.companyName, manager: '负责人' ,id: ''}
      this.departs = tranListToTreeData(result.depts, '')
    },
    addDepartments(node) {
      this.dialogShow = true
      this.node = node
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
